package com.lzw.cpvideoplayer.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.lzw.cpvideoplayer.R;
import com.lzw.cpvideoplayer.data.HeadData;
import com.lzw.cpvideoplayer.data.TitleData;
import com.lzw.cpvideoplayer.mvp.model.LoadHttpData;
import com.lzw.cpvideoplayer.adapter.MyBaseAdapter;
import com.lzw.cpvideoplayer.mvp.presenter.VideoPresenter;
import com.lzw.cpvideoplayer.mvp.view.BaseActivity;
import com.lzw.cpvideoplayer.mvp.view.IBaseView;
import com.lzw.cpvideoplayer.mvp.view.LoadDiaLog;
import com.lzw.cpvideoplayer.ulit.OnItemClickLiner;
import com.lzw.cpvideoplayer.ulit.Ulit;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SearchResultActivity extends BaseActivity<IBaseView, VideoPresenter> implements IBaseView<HeadData> {
    /**
     * 视频搜索结果页面
     * @param savedInstanceState
     */
    private RecyclerView mListView;//列表
    private ImageView mImageBack;//返回按钮
    private TextView mTitle;//标题
    private int count = 1;//当前页数


    private MyBaseAdapter adapter;
//    private HeadData hData;

    private  String search_text;//搜索关键字
    private static final String TAG = "SearchResultActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        initView();
    }

    @Override
    public VideoPresenter createPresenter() {
        return new VideoPresenter();
    }

    private void initView() {

         search_text = getIntent().getStringExtra("search_text");

        mListView = findViewById(R.id.listview);//结果视频列表
        mImageBack = findViewById(R.id.search_back);//返回按钮
        mTitle = findViewById(R.id.search_title);//标题
        mTitle.setText(mTitle.getText()+search_text);

        PutSearchPath(1);//接收并发起请求

            LoadDiaLog.getInstance(SearchResultActivity.this).show();//加载框
        mImageBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//关闭页面
            }
        });
    }

    @Override
    public void ShowViewData(HeadData data) {
         List<TitleData> mList = HeadtoList(data);
        int countMax = Ulit.getCountMax(data.getCount());
        if(count == 1){
            mTitle.setText(mTitle.getText()+"有"+data.getCount()+"条");
            adapter = new MyBaseAdapter(mList, SearchResultActivity.this);
            mListView.setLayoutManager(new LinearLayoutManager(SearchResultActivity.this));
            mListView.setAdapter(adapter);
//            if(data.getCount()%10==0){
//                countMax = data.getCount()/10;
//            }else {
//                countMax = data.getCount()/10+1;
//            }
            adapter.count_max = countMax;
        }
        if(count>1){

            adapter.notifyItem(mList,count,countMax);
            mListView.scrollToPosition(0);//跳转到首行
        }

        LoadDiaLog.getInstance(SearchResultActivity.this).onLoadDismiss();

        adapter.setOnItemClickLiner(new OnItemClickLiner() {
            @Override
            public int OnItemClick(View v, int p) {
                if(v.getId() == R.id.next_count){
                    //下一页
                    if (countMax == count){
                        //到最后一页了
                        Toast.makeText(SearchResultActivity.this,"最后一页了",Toast.LENGTH_SHORT).show();
                    }else {
                        count++;
                        LoadDiaLog.getInstance(SearchResultActivity.this).show();
                        Log.e(TAG, "OnItemClick: "+Ulit.SEARCH_URL+search_text+"/"+count+"/10--"+countMax+"-"+count);
                       // presenter.fatch(SearchResultActivity.this,Ulit.SEARCH_URL+search_text+"/"+count+"/10");
                        PutSearchPath(count);
                    }

                }else {//上一页
                    Log.e(TAG, "OnItemClick: "+countMax+"-"+count);
                    if(count>=2){
                        count--;
                        PutSearchPath(count);
                        LoadDiaLog.getInstance(SearchResultActivity.this).show();
                    }else {
                        Toast.makeText(SearchResultActivity.this,"第一页",Toast.LENGTH_SHORT).show();
                    }

                }
                return 0;
            }
        });

    }

private void PutSearchPath(int count){
    presenter.fatch(SearchResultActivity.this,Ulit.SEARCH_URL+search_text+"/"+count+"/10");
}

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: 关闭 " );
        LoadHttpData.isLoadData = true;
        LoadDiaLog.getInstance(SearchResultActivity.this).onLoadDismiss();//加载框
    }

    public List<TitleData> HeadtoList(HeadData headData){
        List<TitleData> list = new ArrayList<>();
        //数组转换成List容器
        Collections.addAll(list,headData.getData());
        //在最后添加下一页按钮
        TitleData end = new TitleData();
        end.setType(1);
        list.add(end);
        //返回一个完整的搜索结果列表
        return list;
    }
}